var categoryData;
var totalPrice = 0;
//var flag = 1;
angular.module("myApp")
	.controller("HomeCtrl", ["$css", "HomeService", "$timeout", "CommonService",
	function($css, HomeService, $timeout, CommonService) {
		$css.add("./css/home.css");
		$css.add("./css/swiper-3.3.1.min.css");
		var that = this;
		//轮播图
		that.getImg = function() {
			return HomeService.getImg();
		};
		$timeout(function () {
			new Swiper("#home .swiper-container", {
				autoplay: 2000,
				autoplayDiableOnInteraction: false,
				pagination: "#home .swiper-pagination",
				loop: true
			});
		}, 500);
		//分类信息
		that.getBannerData = function() {
			return HomeService.getBannerData();
		};
		//先锋热卖
		that.getHotData = function() {
			return CommonService.getCategoryData("热销榜");
		};
	}])
	.controller("MarketCtrl", ["$css","CommonService", function($css, CommonService) {
		$css.add("./css/market.css");
		var that = this;
		//根据分类请求数据
		that.getCategoryData = function(category) {
			categoryData = CommonService.getCategoryData(category);
		};
		//默认是热销榜
		if (categoryData == undefined) {
			categoryData = CommonService.getCategoryData("热销榜");
		}
		//把请求的数据显示到页面中
		that.getShowData = function() {
			return categoryData;
		};
		//添加商品
		that.addShopping = function(key, sign, price) {
			CommonService.addShopping(key, sign, price);
		};
		//获取添加的商品数量
		that.getNum = function(key) {
			return CommonService.getNum(key);
		};
	
	}])
	.controller("CartCtrl", ["$css","CommonService", function($css, CommonService) {
		$css.add("./css/cart.css");
		var that = this;
		//获取添加的商品
		that.getShopping = function() {
			return CommonService.getShopping();
		}
		//计算总价钱
		that.totalPrice = function() {
			 totalPrice = CommonService.getTotalPirce();
			 return totalPrice;
		};
		//商品的选择框
		that.check = function(index, price, num) {
			CommonService.check(index, price, num);
		};
		//+ - 商品数量
		that.addAndSub = function(key, price, sign) {
			CommonService.addAndSub(key, price, sign);
		};
		var s = 1;
		var sumPrice = totalPrice;
		//全选框
		that.allChoice = function() {
			s == 1 ? $(".choice").addClass("choice2") : $(".choice").removeClass("choice2");
			s *= -1;
			if (s == 1) {
				totalPrice = sumPrice;
				$(".addNum").css("display", "block");
			} else {
				totalPrice = 0
				$(".addNum").css("display", "none");
			}
		}
	}])
	.controller("MineCtrl", ["$css", function($css) {
		$css.add("./css/mine.css");
		var that = this;
	}])
	.controller("DetailCtrl", ["$css", "DetailService", "$stateParams", function($css, DetailService, $stateParams) {
		$css.add("./css/goodsDetail.css");
		var that = this;
		//根据接收的key值去找相应的子页面
		that.goodsDetail = DetailService.getDetailData($stateParams.key);
		//返回上一个页面
		that.goBack = function() {
			window.history.back();
		}
	}])

